package com.fangcun.service.admin.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.api.category.response.CategoryWithChildrenResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.entity.GoodsCategory;
import com.fangcun.mapper.GoodsCategoryMapper;
import com.fangcun.service.admin.GoodsCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService{
	
	@Autowired
	private GoodsCategoryMapper goodsCategoryMapper;

	@Override
	public List<CategoryWithChildrenResponse> listWithChildren(Long teahouseId) throws ProException {
		List<CategoryWithChildrenResponse> list=goodsCategoryMapper.listWithChildren(teahouseId);
		if(list == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public PageInfo<GoodsCategory> list(PageBean<GoodsCategory> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<GoodsCategory> list = goodsCategoryMapper.listForAdmin(request.getParam());
		if(list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public GoodsCategory detail(Long id) throws ProException {
		GoodsCategory cate=goodsCategoryMapper.selectByPrimaryKey(id);
		if(cate == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return cate;
	}

	@Override
	public void update(GoodsCategory request) throws ProException {
		if(goodsCategoryMapper.updateByPrimaryKeySelective(request)<=0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void add(GoodsCategory request) throws ProException {
		if(goodsCategoryMapper.insertSelective(request)<=0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void delete(Long id) throws ProException {
		if(goodsCategoryMapper.deleteByPrimaryKey(id)<=0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	
}
